close all
clear all
clc

cd('D:\Nico\Proyecto\NetBeansProjects\trunk\ProyectoFinalIMRT\mlcfiles')


UM_total = 100;

% --------------------------------------------
%                 carga de segmentos y distribucion
fuga        = 0.02;
archivo     = load('segmentos.txt');
index       = archivo(1,:);
x           = archivo(2:end,:);
aux         = size(x);
cant_s      = aux(2);
% ----------------------------------------------
%                 pesos

pesos(1) = index(1);
for i=2:cant_s,
    pesos(i) = index(i)-index(i-1);
end
UM = pesos*UM_total;


% ---------------------------------------------
%               creacion de matrices
% ---------------------------------------------
Mizq    = zeros(cant_s,80,80);
Mder    = zeros(cant_s,80,80);

kizq= 100;
kder= 50;

for k = 1:cant_s,
    for i=1:10,
        aux_izq     = (40 - 2*(x(i,k)))+1;
        aux_der     = (40 + 2*(x(i+60,k)))+1;
%     pause()
        for j = 1:80,
            if j<aux_izq
                Mizq(k,i*2-1,j)=1;
                Mizq(k,i*2,j)=1;
            end
            if j>aux_der
                Mder(k,i*2-1,j)=1;
                Mder(k,i*2,j)=1;
            end
        end
    end

    for i=11:50,
        aux_izq     = (40 - 2*(x(i,k)))+1;
        aux_der     = (40 + 2*(x(i+60,k)))+1;
        for j = 1:80,
            if j<aux_izq
            Mizq(k,i+10,j)=1;
            end
            if j>aux_der
            Mder(k,i+10,j)=1;
            end
        end
    end
    for i=51:60,
        aux_izq     = (40 - 2*(x(i,k)))+1;
        aux_der     = (40 + 2*(x(i+60,k)))+1;
        for j = 1:80,
            if j<aux_izq
                indice = 60+(i-50)*2-1;
                Mizq(k,indice,j)=1;
                Mizq(k,indice+1,j)=1;
            end
            if j>aux_izq
                indice = 60+(i-50)*2-1;
                Mder(k,indice,j)=1;
                Mder(k,indice+1,j)=1;
            end
        end
    end
end


grafica_seg(UM,cant_s,Mizq,Mder)

M = Mizq+Mder;
for i=1:cant_s,
    for j=1:80,
        for k=1:80,
            if M(i,j,k)==1
               M(i,j,k)=fuga;
            else 
               M(i,j,k)=1;
            end
        end
    end
end

fluencia = zeros(80,80);
for i=1:cant_s,
    aux =zeros(80,80);
    aux(:,:) = M(i,:,:);
    aux = aux*pesos(i);
%     pause
    fluencia = fluencia + aux;
end


% -------------------------------------------
%             graficacion

% % % % kizq= 3;
% % % % kder= 6;
% % % % Mizq = Mizq *kizq;
% % % % Mder = Mder *kder;
% % % % M = Mizq+Mder;
% % % % 
% % % % figure()
% % % % hold on
% % % % for i=1:cant_s,
% % % %     subplot(3,3,i)
% % % %     
% % % %     aux =zeros(80,80);
% % % %     aux(:,:) = M(i,:,:);
% % % %     zoom = 33:47;
% % % %     titulo = num2str(round(UM(i)));
% % % %     image(zoom,zoom,aux)
% % % %     title([titulo,' UM'])
% % % %     colormap(jet(10))
% % % %     axis image        % Set aspect ratio to obtain square pixels
% % % %     grid on
% % % % end
% % % % hold off

% ------------------------------------------------------
%             graficacion de fluencia
% 
% fluencia = fluencia*128;
figure()
hold on
% zoom = 35:48;

image(fluencia)

colormap(gray(128))
axis image        % Set aspect ratio to obtain square pixels
axis([20 60 20 60])

grid on

figure()
[x,y] = meshgrid(-40:1:40-1);
meshc(x,y,fluencia);
% axis([-200 200 -200 200 0 2])


